// DHS Calendar Tutorial - Example 1
PROC GLOBAL
numeric casenum, vcal_len, vcal_used, posn;
string piece, rev_cal, trim_cal;

function string reverseStr(string str);
  string c;
  numeric l = length(str);
  numeric i, j;
  do i = 1 while i <= l/2
    j = l-i+1;
    c = str[i:1];
    str[i:1] = str[j:1];
    str[j:1] = c;
  enddo;
  reverseStr = str;
end;

function string ltrim(string str)
  numeric i;
  do i = 1 while i <= length(str) & str[i:1] = " "; enddo;
  ltrim = str[i:length(str)-i+1];
end;

PROC ZZIR62_FF

PROC WOMAN
preproc
// increment case number
inc(casenum);
// stop after 5 cases - just to be efficient
if casenum > 5 then 
  display("stopping after 6 cases");
  stop(0); 
endif;


// 1) display column 1 of the calendar for the first 6 respondents
if casenum <= 5 then display("VCAL(1) ='%s'",VCAL(1)); endif;


// 2) calculate the full length of calendar by displaying length of strings
vcal_len = length(VCAL(1)); // length of calendar
if casenum <= 5 then display("vcal_len=%d",vcal_len); endif;


// 3) take a piece of a string from column 1
piece = VCAL(1)[44:12]; // a piece of calendar
if casenum <= 5 then display("piece starting at position 44 for length 12='%s'",piece); endif;


// 4) find the position of a substring within a string
posn = pos("P",VCAL(1)); // position in calendar
if casenum <= 5 then display("position of first 'P'=%d",posn); endif;


// 5) reverse a string
rev_cal = reverseStr(VCAL(1)); // calendar from oldest to most recent month (L to R)
if casenum <= 5 then display("rev_cal ='%s'",rev_cal); endif;


// 6) trim a string of leading and trailing spaces
trim_cal = strip(VCAL(1)); // trimmed calendar - removes only trailing spaces
trim_cal = ltrim(trim_cal); // removing leading spaces
if casenum <= 5 then display("trim_cal='%s'",trim_cal); endif;


// 7) display the length of calendar actually used, from the trimmed version
vcal_used = length(trim_cal); // length of calendar used
if casenum <= 5 then display("vcal_used=%d V019=%d",vcal_used,V019); endif;